package com.easondongh.system.controller;

import com.easondongh.system.domain.SysNotice;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysNoticeService;
import com.easondongh.common.shiro.ShiroUtils;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.system.vo.NoticeQueryVo;
import com.easondongh.common.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 系统公告
 * @author EasonDongH
 * @date 2020/5/4 20:16
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private SysNoticeService noticeService;

    /**
     * 新增系统公告
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    public ResponseObj addNotice(@RequestBody SysNotice notice) {
        if (notice == null || StringUtils.isBlank(notice.getTitle()) || StringUtils.isBlank(notice.getContent())) {
            log.warn("系统公告：新增时标题、内容均不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        notice.setCreateTime(new Date());
        notice.setCreateBy(ShiroUtils.getCurrentLoginUser().getLoginName());
        this.noticeService.save(notice);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 修改系统公告
     * @param notice
     * @return
     */
    @PostMapping("updateNotice")
    public ResponseObj updateNotice(@RequestBody SysNotice notice) {
        if (notice == null || notice.getId() == null || StringUtils.isBlank(notice.getTitle()) || StringUtils.isBlank(notice.getContent())) {
            log.warn("系统公告：更新时id、标题、内容均不能为空");
            return new ResponseObj(ResponseEnum.UPDATE_FAIL);
        }
        notice.setUpdateTime(new Date());
        this.noticeService.updateById(notice);
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 批量删除系统公告
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.noticeService.removeByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询系统公告
     * 用于后台查询，会将未启用的公告页查询出来
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<SysNotice>> getByPage(@RequestBody BasePageQueryVo<NoticeQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<SysNotice> result = this.noticeService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    /**
     * 启用当前系统公告
     * @param id
     * @return
     */
    @PutMapping("/enable")
    public ResponseObj enableById(Integer id) {
        if(id != null) {
            this.noticeService.enableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 弃用当前系统公告
     * @param id
     * @return
     */
    @PutMapping("/disable")
    public ResponseObj disableById(Integer id) {
        if(id != null) {
            this.noticeService.disableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 前台展示系统公告
     * @param limit
     * @return
     */
    @GetMapping("/showNotices")
    public ResponseObj<List<SysNotice>> showNotices(Integer limit) {
        List<SysNotice> result = this.noticeService.showNotices(limit);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }
}
